
//
//  JCObjectSerializerConfig.m
//  JCSDK
//
//  Created by master on 16/2/23.
//  Copyright © 2016年 AdMaster. All rights reserved.
//

#import "JCObjectSerializerConfig.h"

#import "JCClassDescription.h"
#import "JCEnumDescription.h"
#import "JCTypeDescription.h"

@interface JCObjectSerializerConfig ()

@property (nonatomic, strong) NSDictionary *classes;
@property (nonatomic, strong) NSDictionary *enums;

@end

@implementation JCObjectSerializerConfig

- (instancetype) initWithDictionary:(NSDictionary *)dictionary {
    if(self = [super init]) {
        NSString *k_superclass = @"superclass";
        NSString *k_classes = @"classes";
        NSString *k_enums = @"enums";
        
        NSMutableDictionary *classDescriptions = [[NSMutableDictionary alloc] init];
        for(NSDictionary *classDict  in dictionary[k_classes]) {
            //get super
            NSString *superclassName = classDict[k_superclass];
            JCClassDescription *superDescription = classDescriptions[superclassName];
            
            //class description
            JCClassDescription *classDescription = [[JCClassDescription alloc] initWithSuperclassDescription:superDescription dictionary:classDict];
            [classDescriptions setValue:classDescription forKey:classDescription.name];
        }
        _classes = classDescriptions;


        NSMutableDictionary *enumDescriptions = [[NSMutableDictionary alloc] init];

        for (NSDictionary *enumDict in dictionary[k_enums]) {
            JCEnumDescription *enumDescription = [[JCEnumDescription alloc] initWithDictionary:enumDict];
            [enumDescriptions setValue:enumDescription forKey:enumDict[@"name"]];
        }
        _enums = enumDescriptions;
        
        
    }
    return self;
}

- (JCClassDescription *)classWithName:(NSString *)className {
    return _classes[className];
}

- (JCEnumDescription *)enumWithName:(NSString *)enumName {
    return _enums[enumName];
}

- (JCTypeDescription *)typeWithName:(NSString *)name {
    
    JCEnumDescription *enumDescription = [self enumWithName:name];
    
    if (enumDescription) {
        return enumDescription;
    }
    
    JCClassDescription *classDescription = [self classWithName:name];
    if (classDescription) {
        return classDescription;
    }
    
    return nil;
}

@end
